<?php
class Category extends CActiveRecord {
	public static function model($className = __CLASS__) {
		parent::model ( $className );
	}
	public function tableName() {
		return '{{category}}';
	}
	public function rules() {
		
		return array (
			
			array (
				
				'parentid,arrchildid,catname,ismenu,letter,orders,modelid,version', 
				'required', 
				'message' => '不能为空' 
			) 
		);
	}
	public function relations() {
		
		return array ();
	}
	public function attributeLabels() {
		return array (
			
			"parentid" => "parentid", 
			"arrchildid" => "arrchildid", 
			"catname" => "catname", 
			"ismenu" => "ismenu", 
			"letter" => "letter", 
			"orders" => "orders", 
			"modelid" => "modelid", 
			"version" => "version" 
		);
	}
	public function getCategory() {
		$version = yii::app ()->session->get ( 'version' );
		$obj = $this->findAll ( array (
			
			"condition" => "parentid=0 and version='$version'" 
		) );
		$ret = CommonFunc::YiiobjectToArray ( $obj, "all" );
		foreach ( $ret as $key => $vs ) {
			$arrchildid = explode ( ",", $vs ['arrchildid'] );
			$child = array ();
			foreach ( $arrchildid as $v ) {
				if ($v) {
					$rows = $this->find ( array (
						
						"condition" => "id=$v" 
					) );
					$child [] = CommonFunc::YiiobjectToArray ( $rows, "one" );
					$ret [$key] ['child'] = $child;
				}
			}
		
		}
		return $ret;
	}
	public function setColumn($array) {
		$version = yii::app ()->session->get ( 'version' );
		$parentid = addslashes ( $array ['categoryid'] );
		$this->parentid = $parentid;
		$arrchildid = 0;
		$this->arrchildid = $arrchildid;
		$this->catname = addslashes ( $array ['catname'] );
		$this->ismenu = addslashes ( $array ['ismenu'] );
		$this->letter = addslashes ( $array ['letter'] );
		$this->modelid = addslashes ( $array ['modelid'] );
		$this->orders = 99;
		if ($this->save () === NULL) {
			return FALSE;
		}
		if ($parentid) {
			$arrchildid = $this->getArrchildid ( $parentid );
			$this->updateByPk ( $parentid, array (
			
			"arrchildid" => $arrchildid 
			) );
		}
		return TRUE;
	}
	public function getCategoryById($id) {
		$version = yii::app ()->session->get ( 'version' );
		$obj = $this->find ( array (
			
			"condition" => "id='$id' and version='$version'" 
		) );
		$ret = CommonFunc::YiiobjectToArray ( $obj, "one" );
		return $ret;
	}
	public function editCategory($array) {
		$id = $array ['id'];
		$parentid = addslashes ( $array ['categoryid'] );
		$ret = $this->updateByPk ( $id, array (
			
			"catname" => addslashes ( $array ['catname'] ), 
			"ismenu" => addslashes ( $array ['ismenu'] ), 
			"letter" => addslashes ( $array ['letter'] ), 
			"modelid" => addslashes ( $array ['modelid'] ), 
			"parentid" => $parentid 
		) );
		if ($parentid) {
			$arrchildid = $this->getArrchildid ( $parentid );
			$this->updateByPk ( $parentid, array (
				
				"arrchildid" => $arrchildid 
			) );
			$oldparentid = $array ['oldparentid'];
			$oldarrchildid = $this->getArrchildid ( $oldparentid );
			$this->updateByPk ( $oldparentid, array (
				
				"arrchildid" => $oldarrchildid 
			) );
		}
		return $oldparentid;
		return TRUE;
	}
	
	public function getArrchildid($parentid) {
		$version = yii::app ()->session->get ( 'version' );
		$arrchildid = 0;
		$ret = $this->findAll ( array (
			
			"condition" => "parentid='$parentid' and version='$version'" 
		) );
		$rows = CommonFunc::YiiobjectToArray ( $ret, "all" );
		foreach ( $ret as $v ) {
			$child [] = $v ['id'];
		}
		$arrchildid = implode ( ",", $child );
		return $arrchildid;
	}

}
?>